import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kegelutils/bean/bean_day_event.dart';
import 'package:kegelutils/controller_events.dart';
import 'package:kegelutils/controller_main.dart';
import 'package:kegelutils/dailogs.dart';
import 'package:kegelutils/page_calendar.dart';
import 'package:kegelutils/page_plan.dart';
import 'package:kegelutils/view_play_info.dart';
import 'package:table_calendar/table_calendar.dart';

final Gradient gradient = const LinearGradient(
  begin: Alignment.centerLeft,
  end: Alignment.centerRight,
  colors: [
    Colors.red,
    Colors.purple,
  ],
);
final Shader shader = gradient.createShader(
    Rect.fromLTWH(0, 0, Get.mediaQuery.size.width, Get.mediaQuery.size.height));

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    MainController.to.setupPlanInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: SafeArea(
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 10.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const SizedBox(height: 30.0),
              Stack(
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: GestureDetector(
                      onTap: () {
                        EventsController.to.monthEvents.value =
                            MainController.getSaveEvents();
                        EventsController.to.monthEvents.refresh();

                        List<Event> todayEvents = [];
                        int todayDuration = 0;
                        for (Event event in EventsController.to.monthEvents) {
                          if (isSameDay(
                              DateTime.fromMillisecondsSinceEpoch(
                                  event.createTime!),
                              DateTime.now())) {
                            todayDuration += event.duration!;
                          }
                        }
                        popDialog(
                            Center(
                              child: RichText(
                                text: TextSpan(
                                  text: '今日坚持锻炼  ',
                                  style: const TextStyle(
                                    fontSize: 18.0,
                                    color: Colors.black87,
                                  ),
                                  children: [
                                    TextSpan(
                                      text: '${todayDuration ~/ 60}',
                                      style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 40.0,
                                        foreground: Paint()..shader = shader,
                                      ),
                                    ),
                                    const TextSpan(
                                      text: '  分钟。',
                                      style: TextStyle(
                                        fontSize: 18.0,
                                        color: Colors.black87,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ), onPositiveTap: () {
                          Get.back();
                        });
                      },
                      child: Container(
                        width: 40,
                        height: 40,
                        decoration: BoxDecoration(
                          color: Colors.red,
                          borderRadius: BorderRadius.circular(50.0),
                        ),
                        child: const Center(
                          child: Icon(
                            Icons.thumb_up_alt_outlined,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ),
                  Align(
                    alignment: Alignment.center,
                    child: Text(
                      '提肛运动助手',
                      style: TextStyle(
                        fontSize: 30.0,
                        fontWeight: FontWeight.bold,
                        foreground: Paint()..shader = shader,
                      ),
                    ),
                  ),
                  Align(
                    alignment: Alignment.centerRight,
                    child: GestureDetector(
                      onTap: () {
                        Get.to(
                          () => const CalendarPage(),
                          transition: Transition.rightToLeft,
                        );
                      },
                      child: Container(
                        width: 40,
                        height: 40,
                        decoration: BoxDecoration(
                          color: Colors.purple,
                          borderRadius: BorderRadius.circular(50.0),
                        ),
                        child: const Center(
                          child: Icon(
                            Icons.calendar_today_rounded,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              Expanded(
                flex: 3,
                child: PageView(
                  controller: MainController.to.pageController,
                  children: [
                    PlanPage(0),
                    PlanPage(1),
                    PlanPage(2),
                    PlanPage(3),
                  ],
                ),
              ),
              Expanded(
                flex: 1,
                child: Stack(
                  children: [
                    Positioned.fill(
                      child: PageView(
                        controller: MainController.to.remainTimeController,
                        children: [
                          PlayInfoView(0),
                          PlayInfoView(1),
                          PlayInfoView(2),
                          PlayInfoView(3),
                        ],
                      ),
                    ),
                    Align(
                      alignment: Alignment.topLeft,
                      child: buildLeft(),
                    ),
                    Align(
                      alignment: Alignment.topRight,
                      child: buildRight(),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  ///构建向左点击
  Widget buildLeft() {
    return GestureDetector(
      onTap: () {
        int curIndex = MainController.to.planIndex.value;
        if (curIndex > 0) {
          MainController.to.pageController.animateToPage(
            curIndex - 1,
            duration: Duration(milliseconds: 500),
            curve: Curves.easeIn,
          );
          MainController.to.remainTimeController.animateToPage(
            curIndex - 1,
            duration: Duration(milliseconds: 500),
            curve: Curves.easeIn,
          );
        }
      },
      child: Container(
        padding: const EdgeInsets.all(10.0),
        color: Colors.transparent,
        child: const Icon(
          Icons.chevron_left_rounded,
          size: 60.0,
          color: Colors.red,
        ),
      ),
    );
  }

  ///构建向右点击
  Widget buildRight() {
    return GestureDetector(
      onTap: () {
        int curIndex = MainController.to.planIndex.value;
        if (curIndex < 3) {
          print('tap right dst index= ${curIndex + 1}');
          MainController.to.pageController.animateToPage(
            curIndex + 1,
            duration: Duration(milliseconds: 500),
            curve: Curves.easeIn,
          );
          MainController.to.remainTimeController.animateToPage(
            curIndex + 1,
            duration: Duration(milliseconds: 500),
            curve: Curves.easeIn,
          );
        }
      },
      child: Container(
        padding: const EdgeInsets.all(10.0),
        color: Colors.transparent,
        child: const Icon(
          Icons.chevron_right_rounded,
          size: 60.0,
          color: Colors.purple,
        ),
      ),
    );
  }
}
